﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace Demo
{
    public partial class ChartAlightmentDemo : Form
    {
        public ChartAlightmentDemo()
        {
            InitializeComponent();
        }

        private void ChartAlightmentDemo_Load(object sender, EventArgs e)
        {
            var names = Enum.GetNames(typeof(AreaAlignmentOrientations));
            cbOrient1.Items.AddRange(names);
            cbOrient2.Items.AddRange(names);
            names = Enum.GetNames(typeof(AreaAlignmentStyles)); ;
            cbStyle1.Items.AddRange(names);
            cbStyle2.Items.AddRange(names);
            names = chart1.ChartAreas.Select(a => a.Name).ToArray();
            cbWith1.Items.AddRange(names);
            cbWith2.Items.AddRange(names);
            Random random = new Random();
            for (int i = 0; i < 10; i++)
            {
                var val = random.NextDouble();
                chart1.Series[0].Points.Add(val);
                var v = random.NextDouble();
                chart1.Series[1].Points.Add(v);
            }
            chart1.ChartAreas[0].BackColor = Color.Transparent;
            chart1.ChartAreas[1].BackColor = Color.Transparent;

            chkAutoPosition.Checked = chart1.ChartAreas[0].Position.Auto;
            txtPos.Text = GetSizeString(chart1.ChartAreas[0].Position.ToRectangleF());

            chkAutoPos2.Checked = chart1.ChartAreas[1].Position.Auto;
            txtPos2.Text = GetSizeString(chart1.ChartAreas[1].Position.ToRectangleF());
        }

        private void cbOrient1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbOrient1.SelectedIndex < 0) return;
            var or = (AreaAlignmentOrientations)cbOrient1.SelectedIndex;
            chart1.ChartAreas[0].AlignmentOrientation = or;
        }

        private void cbOrient2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbOrient2.SelectedIndex < 0) return;
            var or = (AreaAlignmentOrientations)cbOrient2.SelectedIndex;
            chart1.ChartAreas[1].AlignmentOrientation = or;
            chkAutoPos2.Checked = chart1.ChartAreas[1].Position.Auto;
            txtPos2.Text = GetSizeString(chart1.ChartAreas[1].Position.ToRectangleF());
        }

        private void cbStyle1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbStyle1.SelectedIndex < 0) return;
            var or = (AreaAlignmentStyles)cbStyle1.SelectedIndex;
            chart1.ChartAreas[0].AlignmentStyle = or;
        }

        private void cbStyle2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbStyle2.SelectedIndex < 0) return;
            var or = (AreaAlignmentStyles)cbStyle2.SelectedIndex;
            chart1.ChartAreas[1].AlignmentStyle = or;
            chkAutoPos2.Checked = chart1.ChartAreas[1].Position.Auto;
            txtPos2.Text = GetSizeString(chart1.ChartAreas[1].Position.ToRectangleF());
        }

        private void cbWith1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbWith1.SelectedIndex < 0) return;
            chart1.ChartAreas[0].AlignWithChartArea = cbWith1.Text;
        }

        private void cbWith2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbWith2.SelectedIndex < 0) return;
            chart1.ChartAreas[1].AlignWithChartArea = cbWith2.Text;
            chkAutoPos2.Checked = chart1.ChartAreas[1].Position.Auto;
            txtPos2.Text = GetSizeString(chart1.ChartAreas[1].Position.ToRectangleF());
        }

        private void chkShowLegend_CheckedChanged(object sender, EventArgs e)
        {
            foreach (var se in chart1.Series)
            {
                se.IsVisibleInLegend = chkShowLegend.Checked;
            }
        }

        private void chkDockLegend_CheckedChanged(object sender, EventArgs e)
        {
            if(chkDockLegend.Checked)
            {
                chart1.Legends[0].DockedToChartArea = "ChartArea1";
                chart1.Legends[1].DockedToChartArea = "ChartArea2";
            }
            else
            {
                chart1.Legends[0].DockedToChartArea = "NotSet";
                chart1.Legends[1].DockedToChartArea = "NotSet";
            }
        }

        private void chkAutoPosition_CheckedChanged(object sender, EventArgs e)
        {
            chart1.ChartAreas[0].Position.Auto = chkAutoPosition.Checked;
            txtPos.Text = GetSizeString(chart1.ChartAreas[0].Position.ToRectangleF());
        }
        private string GetSizeString(RectangleF rect)
        {
            return $"{rect.X}, {rect.Y}, {rect.Width}, {rect.Height}";
        }

        private void chkAutoPos2_CheckedChanged(object sender, EventArgs e)
        {
            chart1.ChartAreas[1].Position.Auto = chkAutoPos2.Checked;
            txtPos2.Text = GetSizeString(chart1.ChartAreas[1].Position.ToRectangleF());
        }

        private void txtPos_Validated(object sender, EventArgs e)
        {
            var sgs = txtPos.Text.Split(new char[] { ',', ';', ' ', '，', '；' }, StringSplitOptions.RemoveEmptyEntries);
            if (sgs.Length < 4) return;
            if(float.TryParse(sgs[0],out float x)&& float.TryParse(sgs[1], out float y)&& float.TryParse(sgs[2], out float w)&& float.TryParse(sgs[3], out float h))
            {
                chart1.ChartAreas[0].Position.Auto = false;
                chart1.ChartAreas[0].Position.X = x;
                chart1.ChartAreas[0].Position.Y = y;
                chart1.ChartAreas[0].Position.Width = w;
                chart1.ChartAreas[0].Position.Height = h;

            }
            else
            {
                chart1.ChartAreas[0].Position.Auto = true;
            }
            chkAutoPosition.Checked = chart1.ChartAreas[0].Position.Auto;
            txtPos.Text = GetSizeString(chart1.ChartAreas[0].Position.ToRectangleF());
        }

        private void txtPos2_Validated(object sender, EventArgs e)
        {
            var sgs = txtPos2.Text.Split(new char[] { ',', ';', ' ', '，', '；' }, StringSplitOptions.RemoveEmptyEntries);
            if (sgs.Length < 4) return;
            if (float.TryParse(sgs[0], out float x) && float.TryParse(sgs[1], out float y) && float.TryParse(sgs[2], out float w) && float.TryParse(sgs[3], out float h))
            {
                chart1.ChartAreas[1].Position.Auto = false;
                chart1.ChartAreas[1].Position.X = x;
                chart1.ChartAreas[1].Position.Y = y;
                chart1.ChartAreas[1].Position.Width = w;
                chart1.ChartAreas[1].Position.Height = h;

            }
            else
            {
                chart1.ChartAreas[1].Position.Auto = true;
            }
            chkAutoPos2.Checked = chart1.ChartAreas[1].Position.Auto;
            txtPos2.Text = GetSizeString(chart1.ChartAreas[1].Position.ToRectangleF());
        }
    }
}
